﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgressControls;
using SCResources.ImdbSerieService;

namespace Serie_Counter.Forms
{
    public partial class SearchResultsForm : Form
    {
        private ImdbServices imdbServices;
        private SeriesResultset seriesResultset;

        #region Properties

        public ImdbServices ImdbServices
        {
            get
            {
                return imdbServices;
            }
            set
            {
                imdbServices = value;
            }
        }

        public int SerieId
        {
            get
            {
                int id;
                if (int.TryParse(pictureBoxCover.Tag.ToString(), out id))
                {
                    return id;
                }
                else
                {
                    return -1;
                }
            }
        }

        public Image SerieCover
        {
            get
            {
                return pictureBoxCover.Image;
            }
        }

        #endregion


        public SearchResultsForm(SeriesResultset seriesResultset)
        {
            InitializeComponent();

            imdbServices = new ImdbServices();

            this.seriesResultset = seriesResultset;
            
            IniTreeView();
        }

        private void IniTreeView()
        {
            treeViewResults.BeginUpdate();
            treeViewResults.Nodes.Clear();

            if (seriesResultset.PopularTitles != null)
                treeViewResults.Nodes.Add("Popular titles")
                    .Nodes.AddRange(seriesResultset.PopularTitles.Select(X => new TreeNode() { Text = X.Title, Tag = X.Id }).ToArray());

            if (seriesResultset.ExactMatches != null)
                treeViewResults.Nodes.Add("Exact matches")
                    .Nodes.AddRange(seriesResultset.ExactMatches.Select(X => new TreeNode() { Text = X.Title, Tag = X.Id }).ToArray());

            if (seriesResultset.PartialMatches != null)
                treeViewResults.Nodes.Add("Partial matches")
                    .Nodes.AddRange(seriesResultset.PartialMatches.Select(X => new TreeNode() { Text = X.Title, Tag = X.Id }).ToArray());

            treeViewResults.EndUpdate();
        }

        private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int id;
            if (e.Node.Tag == null) return;
            if (int.TryParse(e.Node.Tag.ToString(), out id))
            {
                pictureBoxCover.Image = imdbServices.GetSerieCover(e.Node.Tag.ToString());
                pictureBoxCover.Tag = e.Node.Tag.ToString();
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            int id;
            if (int.TryParse(pictureBoxCover.Tag.ToString(), out id))
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            //todo: show message / abort submit ...
        }
    }
}
